﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;

namespace aucn.web.au.code
{
    public class JointDepartment
    {
        private string _deptId, _deptName;
        private List<JointProgram> _programList;

        public string DepartmentID { get { return this._deptId; } }
        public string DepartmentName { get { return this._deptName; } }
        public List<JointProgram> ProgramList { get { return this._programList; } }

        public JointDepartment(string departmentId, string departmentName)
        {
            this._deptId = departmentId;
            this._deptName = departmentName;
            this._programList = new List<JointProgram>();
        }

        public static JointDepartment FromXml(string departmentId)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/master.xml"));
            XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
            xm.AddNamespace("x", "http://tempuri.org/master.xsd");

            //JointDepartment jd = new JointDepartment(departmentId, );
            XmlNode deptNode = doc.DocumentElement.SelectSingleNode("x:department[@name='" + departmentId + "']", xm);
            JointDepartment jd = new JointDepartment(departmentId, deptNode.SelectSingleNode("x:name", xm).InnerText);
            XmlNodeList programList = deptNode.SelectNodes("x:programlist/x:program", xm);
            foreach (XmlNode progNode in programList)
            {
                XmlNodeList universityList = progNode.SelectNodes("x:universities/x:university", xm);
                JointProgram jp = new JointProgram(progNode.Attributes["name"].Value, progNode.SelectSingleNode("x:name", xm).InnerText);

                foreach (XmlNode uni in universityList)
                {
                    string uniName;
                    try { uniName = uni.SelectSingleNode("x:universityname", xm).InnerText; }
                    catch { uniName = string.Empty; }

                    XmlNodeList subjectNodeList = uni.SelectNodes("x:subjectlist/x:subject", xm);
                    JointUniversity ju = new JointUniversity(uni.Attributes["name"].Value, uniName);
                    foreach (XmlNode subject in subjectNodeList)
                    {
                        ju.SubjectList.Add(subject.Attributes["name"].Value);
                    }

                    jp.UniversityList.Add(ju);
                }

                jd.ProgramList.Add(jp);
            }

            return jd;
        }
    }

    public class JointProgram
    {
        private string _progId, _progName;
        private List<JointUniversity> _universityList;

        public string ProgramID { get { return this._progId; } set { this._progId = value; } }
        public string ProgramName { get { return this._progName; } set { this._progName = value; } }
        public List<JointUniversity> UniversityList { get { return this._universityList; } }

        public JointProgram(string progId, string progName)
        {
            this._progId = progId;
            this._progName = progName;
            this._universityList = new List<JointUniversity>();
        }

        public static JointProgram FromXml(string department, string programId)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/master.xml"));
            XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
            xm.AddNamespace("x", "http://tempuri.org/master.xsd");
            XmlNode progNode = doc.DocumentElement.SelectSingleNode("x:department[@name='" + department + "']/x:programlist/x:program[@name='" + programId + "']", xm);
            XmlNodeList universityList = progNode.SelectNodes("x:universities/x:university", xm);

            JointProgram jp = new JointProgram(programId, progNode.SelectSingleNode("x:name", xm).InnerText);

            foreach (XmlNode uni in universityList)
            {
                string uniName;
                try { uniName = uni.SelectSingleNode("x:universityname", xm).InnerText; }
                catch { uniName = string.Empty; }

                XmlNodeList subjectNodeList = uni.SelectNodes("x:subjectlist/x:subject", xm);
                JointUniversity ju = new JointUniversity(uni.Attributes["name"].Value, uniName);
                foreach (XmlNode subject in subjectNodeList)
                {
                    ju.SubjectList.Add(subject.Attributes["name"].Value);
                }

                jp.UniversityList.Add(ju);
            }

            return jp;
        }
    }

    public class JointUniversity
    {
        private string _uniId, _uniName;
        private List<string> _subujectList;

        public string UniversityID { get { return this._uniId; } set { this._uniId = value; } }
        public string UniversityName { get { return this._uniName; } set { this._uniName = value; } }
        public List<string> SubjectList { get { return this._subujectList; } }

        public JointUniversity(string id, string name)
        {
            this._uniId = id;
            this._uniName = name;
            this._subujectList = new List<string>();
        }
    }
}
